/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2020 Philippe Proulx <pproulx@efficios.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 *
 * The following code was generated by barectf v3.1.2
 * on 2025-09-05T16:28:39.589080.
 *
 * For more details, see <https://barectf.org/>.
 */

#include <stdint.h>
#include <string.h>
#include <assert.h>

#include "barectf.h"
#include "barectf-bitfield.h"

#define _ALIGN(_at_var, _align)						\
	do {								\
		(_at_var) = ((_at_var) + ((_align) - 1)) & -(_align);	\
	} while (0)

#ifdef __cplusplus
# define _TO_VOID_PTR(_value)		static_cast<void *>(_value)
# define _FROM_VOID_PTR(_type, _value)	static_cast<_type *>(_value)
#else
# define _TO_VOID_PTR(_value)		((void *) (_value))
# define _FROM_VOID_PTR(_type, _value)	((_type *) (_value))
#endif

#define _BITS_TO_BYTES(_x)	((_x) >> 3)
#define _BYTES_TO_BITS(_x)	((_x) << 3)

union _f2u {
	float f;
	uint32_t u;
};

union _d2u {
	double f;
	uint64_t u;
};

uint32_t barectf_packet_size(const void * const vctx)
{
	return _FROM_VOID_PTR(const struct barectf_ctx, vctx)->packet_size;
}

int barectf_packet_is_full(const void * const vctx)
{
	const struct barectf_ctx * const ctx = _FROM_VOID_PTR(const struct barectf_ctx, vctx);

	return ctx->at == ctx->packet_size;
}

int barectf_packet_is_empty(const void * const vctx)
{
	const struct barectf_ctx * const ctx = _FROM_VOID_PTR(const struct barectf_ctx, vctx);

	return ctx->at <= ctx->off_content;
}

uint32_t barectf_packet_events_discarded(const void * const vctx)
{
	return _FROM_VOID_PTR(const struct barectf_ctx, vctx)->events_discarded;
}

uint32_t barectf_discarded_event_records_count(const void * const vctx)
{
	return barectf_packet_events_discarded(vctx);
}

uint32_t barectf_packet_sequence_number(const void * const vctx)
{
	return _FROM_VOID_PTR(const struct barectf_ctx, vctx)->sequence_number;
}

uint8_t *barectf_packet_buf(const void * const vctx)
{
	return _FROM_VOID_PTR(const struct barectf_ctx, vctx)->buf;
}

uint8_t *barectf_packet_buf_addr(const void * const vctx)
{
	return barectf_packet_buf(vctx);
}

uint32_t barectf_packet_buf_size(const void * const vctx)
{
	const struct barectf_ctx * const ctx = _FROM_VOID_PTR(const struct barectf_ctx, vctx);

	return _BITS_TO_BYTES(ctx->packet_size);
}

void barectf_packet_set_buf(void * const vctx, uint8_t * const buf,
		const uint32_t buf_size)
{
	struct barectf_ctx * const ctx = _FROM_VOID_PTR(struct barectf_ctx, vctx);

	ctx->buf = buf;

	if (ctx->at == ctx->packet_size) {
		/* Keep full packet state */
		ctx->at = _BYTES_TO_BITS(buf_size);
	}

	ctx->packet_size = _BYTES_TO_BITS(buf_size);
}

int barectf_packet_is_open(const void * const vctx)
{
	return _FROM_VOID_PTR(const struct barectf_ctx, vctx)->packet_is_open;
}

int barectf_is_in_tracing_section(const void * const vctx)
{
	return _FROM_VOID_PTR(const struct barectf_ctx, vctx)->in_tracing_section;
}

volatile const int *barectf_is_in_tracing_section_ptr(const void * const vctx)
{
	return &_FROM_VOID_PTR(const struct barectf_ctx, vctx)->in_tracing_section;
}

int barectf_is_tracing_enabled(const void * const vctx)
{
	return _FROM_VOID_PTR(const struct barectf_ctx, vctx)->is_tracing_enabled;
}

void barectf_enable_tracing(void * const vctx, const int enable)
{
	_FROM_VOID_PTR(struct barectf_ctx, vctx)->is_tracing_enabled = enable;
}

static
void _write_c_str(struct barectf_ctx * const ctx, const char * const src)
{
	const uint32_t sz = strlen(src) + 1;

	memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], src, sz);
	ctx->at += _BYTES_TO_BITS(sz);
}

static
int _reserve_er_space(void * const vctx, const uint32_t er_size)
{
	int ret;
	struct barectf_ctx * const ctx = _FROM_VOID_PTR(struct barectf_ctx, vctx);

	/* Event _cannot_ fit? */
	if (er_size > (ctx->packet_size - ctx->off_content)) {
		goto no_space;
	}

	/* Packet is full? */
	if (barectf_packet_is_full(ctx)) {
		/* Yes: is the back end full? */
		if (ctx->cbs.is_backend_full(ctx->data)) {
			/* Yes: discard event record */
			goto no_space;
		}

		/* Back-end is _not_ full: open new packet */
		ctx->use_cur_last_event_ts = 1;
		ctx->cbs.open_packet(ctx->data);
		ctx->use_cur_last_event_ts = 0;
	}

	/* Event fits the current packet? */
	if (er_size > (ctx->packet_size - ctx->at)) {
		/* No: close packet now */
		ctx->use_cur_last_event_ts = 1;
		ctx->cbs.close_packet(ctx->data);
		ctx->use_cur_last_event_ts = 0;

		/* Is the back end full? */
		if (ctx->cbs.is_backend_full(ctx->data)) {
			/* Yes: discard event record */
			goto no_space;
		}

		/* Back-end is _not_ full: open new packet */
		ctx->use_cur_last_event_ts = 1;
		ctx->cbs.open_packet(ctx->data);
		ctx->use_cur_last_event_ts = 0;
		assert(er_size <= (ctx->packet_size - ctx->at));
	}

	ret = 1;
	goto end;

no_space:
	ctx->events_discarded++;
	ret = 0;

end:
	return ret;
}

static
void _commit_er(void * const vctx)
{
	struct barectf_ctx * const ctx = _FROM_VOID_PTR(struct barectf_ctx, vctx);

	/* Is the packet full? */
	if (barectf_packet_is_full(ctx)) {
		/* Yes: close it now */
		ctx->cbs.close_packet(ctx->data);
	}
}

/* Initialize context */
void barectf_init(void *vctx,
	uint8_t * const buf, const uint32_t buf_size,
	const struct barectf_platform_callbacks cbs, void * const data)
{
	struct barectf_ctx * const ctx = _FROM_VOID_PTR(struct barectf_ctx, vctx);
	ctx->cbs = cbs;
	ctx->data = data;
	ctx->buf = buf;
	ctx->packet_size = _BYTES_TO_BITS(buf_size);
	ctx->at = 0;
	ctx->events_discarded = 0;
	ctx->sequence_number = 0;
	ctx->packet_is_open = 0;
	ctx->in_tracing_section = 0;
	ctx->is_tracing_enabled = 1;
	ctx->use_cur_last_event_ts = 0;
}

/* Open packet for data stream type `default` */
void barectf_default_open_packet(
	struct barectf_default_ctx * const sctx)
{
	struct barectf_ctx * const ctx = &sctx->parent;
	const uint64_t ts = ctx->use_cur_last_event_ts ?
		sctx->cur_last_event_ts :
		ctx->cbs.default_clock_get_value(ctx->data);
	const int saved_in_tracing_section = ctx->in_tracing_section;

	/*
	 * This function is either called by a tracing function, or
	 * directly by the platform.
	 *
	 * If it's called by a tracing function, then
	 * `ctx->in_tracing_section` is 1, so it's safe to open
	 * the packet here (alter the packet), even if tracing was
	 * disabled in the meantime because we're already in a tracing
	 * section (which finishes at the end of the tracing function
	 * call).
	 *
	 * If it's called directly by the platform, then if tracing is
	 * disabled, we don't want to alter the packet, and return
	 * immediately.
	 */
	if (!ctx->is_tracing_enabled && !saved_in_tracing_section) {
		ctx->in_tracing_section = 0;
		goto end;
	}

	/* We can alter the packet */
	ctx->in_tracing_section = 1;

	/* Do not open a packet that is already open */
	if (ctx->packet_is_open) {
		ctx->in_tracing_section = saved_in_tracing_section;
		goto end;
	}

	ctx->at = 0;

	/* Write packet header structure */
	{
		/* Align for packet header structure */
		_ALIGN(ctx->at, 8);

		/* Align for `magic` field */
		_ALIGN(ctx->at, 8);

		/* Write magic number field */
		{
			const uint32_t tmp_val = (uint32_t) 0xc1fc1fc1UL;

			memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
			ctx->at += 32;
		}

		/* Align for `stream_id` field */
		_ALIGN(ctx->at, 8);

		/* Write data stream type ID field */
		{
			const uint64_t tmp_val = (uint64_t) 0;

			memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
			ctx->at += 64;
		}
	}

	/* Write packet context structure */
	{
		/* Align for packet context structure */
		_ALIGN(ctx->at, 8);

		/* Align for `packet_size` field */
		_ALIGN(ctx->at, 8);

		/* Write packet total size field */
		{
			const uint64_t tmp_val = (uint64_t) ctx->packet_size;

			memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
			ctx->at += 64;
		}

		/* Align for `content_size` field */
		_ALIGN(ctx->at, 8);

		/* Do not write `content_size` field; save its offset */
		sctx->off_pc_content_size = ctx->at;
		ctx->at += 64;

		/* Align for `timestamp_begin` field */
		_ALIGN(ctx->at, 8);

		/* Write beginning timestamp field */
		{
			const uint64_t tmp_val = (uint64_t) ts;

			memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
			ctx->at += 64;
		}

		/* Align for `timestamp_end` field */
		_ALIGN(ctx->at, 8);

		/* Do not write `timestamp_end` field; save its offset */
		sctx->off_pc_timestamp_end = ctx->at;
		ctx->at += 64;

		/* Align for `events_discarded` field */
		_ALIGN(ctx->at, 8);

		/* Do not write `events_discarded` field; save its offset */
		sctx->off_pc_events_discarded = ctx->at;
		ctx->at += 64;
	}

	/* Save content beginning's offset */
	ctx->off_content = ctx->at;

	/* Mark current packet as open */
	ctx->packet_is_open = 1;

	/* Not tracing anymore */
	ctx->in_tracing_section = saved_in_tracing_section;

end:
	return;
}

/* Close packet for data stream type `default` */
void barectf_default_close_packet(struct barectf_default_ctx * const sctx)
{
	struct barectf_ctx * const ctx = &sctx->parent;
	const uint64_t ts = ctx->use_cur_last_event_ts ?
		sctx->cur_last_event_ts :
		ctx->cbs.default_clock_get_value(ctx->data);
	const int saved_in_tracing_section = ctx->in_tracing_section;

	/*
	 * This function is either called by a tracing function, or
	 * directly by the platform.
	 *
	 * If it's called by a tracing function, then
	 * `ctx->in_tracing_section` is 1, so it's safe to close
	 * the packet here (alter the packet), even if tracing was
	 * disabled in the meantime, because we're already in a tracing
	 * section (which finishes at the end of the tracing function
	 * call).
	 *
	 * If it's called directly by the platform, then if tracing is
	 * disabled, we don't want to alter the packet, and return
	 * immediately.
	 */
	if (!ctx->is_tracing_enabled && !saved_in_tracing_section) {
		ctx->in_tracing_section = 0;
		goto end;
	}

	/* We can alter the packet */
	ctx->in_tracing_section = 1;

	/* Do not close a packet that is not open */
	if (!ctx->packet_is_open) {
		ctx->in_tracing_section = saved_in_tracing_section;
		goto end;
	}

	/* Save content size */
	ctx->content_size = ctx->at;

	/* Go back to `timestamp_end` field offset */
	ctx->at = sctx->off_pc_timestamp_end;

	/* Write `timestamp_end` field */
	{
		const uint64_t tmp_val = (uint64_t) ts;

		memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
		ctx->at += 64;
	}

	/* Go back to `content_size` field offset */
	ctx->at = sctx->off_pc_content_size;

	/* Write `content_size` field */
	{
		const uint64_t tmp_val = (uint64_t) ctx->content_size;

		memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
		ctx->at += 64;
	}

	/* Go back to `events_discarded` field offset */
	ctx->at = sctx->off_pc_events_discarded;

	/* Write `events_discarded` field */
	{
		const uint64_t tmp_val = (uint64_t) ctx->events_discarded;

		memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
		ctx->at += 64;
	}

	/* Go back to end of packet */
	ctx->at = ctx->packet_size;

	/* Mark packet as closed */
	ctx->packet_is_open = 0;

	/* Not tracing anymore */
	ctx->in_tracing_section = saved_in_tracing_section;

end:
	return;
}

static void _serialize_er_header_default(void * const vctx,
	const uint32_t ert_id)
{
	struct barectf_ctx * const ctx = _FROM_VOID_PTR(struct barectf_ctx, vctx);
	struct barectf_default_ctx * const sctx = _FROM_VOID_PTR(struct barectf_default_ctx, vctx);
	const uint64_t ts = sctx->cur_last_event_ts;

	/* Write header structure */
	{
		/* Align for header structure */
		_ALIGN(ctx->at, 8);

		/* Align for `id` field */
		_ALIGN(ctx->at, 8);

		/* Write event record type ID field */
		{
			const uint64_t tmp_val = (uint64_t) ert_id;

			memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
			ctx->at += 64;
		}

		/* Align for `timestamp` field */
		_ALIGN(ctx->at, 8);

		/* Write timestamp field */
		{
			const uint64_t tmp_val = (uint64_t) ts;

			memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
			ctx->at += 64;
		}
	}
}

static void _serialize_er_default_inputData(void * const vctx,
	const char * const p_typeName,
	const char * const p_instanceName,
	const uint64_t p_dataId,
	const char * const p_value)
{
	struct barectf_ctx * const ctx = _FROM_VOID_PTR(struct barectf_ctx, vctx);

	/* Serialize header */
	_serialize_er_header_default(ctx, 0);

	/* Write payload structure */
	{
		/* Align for payload structure */
		_ALIGN(ctx->at, 64);

		/* Align for `typeName` field */
		_ALIGN(ctx->at, 8);

		/* Write `typeName` field */
		_write_c_str(ctx, p_typeName);

		/* Align for `instanceName` field */
		_ALIGN(ctx->at, 8);

		/* Write `instanceName` field */
		_write_c_str(ctx, p_instanceName);

		/* Align for `dataId` field */
		_ALIGN(ctx->at, 64);

		/* Write `dataId` field */
		{
			const uint64_t tmp_val = (uint64_t) p_dataId;

			memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
			ctx->at += 64;
		}

		/* Align for `value` field */
		_ALIGN(ctx->at, 8);

		/* Write `value` field */
		_write_c_str(ctx, p_value);
	}
}

static void _serialize_er_default_instanceData(void * const vctx,
	const char * const p_typeName,
	const char * const p_instanceName,
	const uint32_t p___inputs_len,
	const char * const * const p_inputs,
	const uint32_t p___outputs_len,
	const char * const * const p_outputs,
	const uint32_t p___internal_len,
	const char * const * const p_internal,
	const uint32_t p___internalFB_len,
	const char * const * const p_internalFB)
{
	struct barectf_ctx * const ctx = _FROM_VOID_PTR(struct barectf_ctx, vctx);

	/* Serialize header */
	_serialize_er_header_default(ctx, 1);

	/* Write payload structure */
	{
		/* Align for payload structure */
		_ALIGN(ctx->at, 8);

		/* Align for `typeName` field */
		_ALIGN(ctx->at, 8);

		/* Write `typeName` field */
		_write_c_str(ctx, p_typeName);

		/* Align for `instanceName` field */
		_ALIGN(ctx->at, 8);

		/* Write `instanceName` field */
		_write_c_str(ctx, p_instanceName);

		/* Align for `__inputs_len` field */
		_ALIGN(ctx->at, 8);

		/* Write `__inputs_len` field */
		{
			const uint32_t tmp_val = (uint32_t) p___inputs_len;

			memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
			ctx->at += 32;
		}

		/* Align for `inputs` field */
		_ALIGN(ctx->at, 8);

		/* Write `inputs` field */
		{
			uint32_t i;

			for (i = 0; i < (uint32_t) p___inputs_len; ++i) {
				/* Align for `[i]` field */
				_ALIGN(ctx->at, 8);

				/* Write `[i]` field */
				_write_c_str(ctx, p_inputs[i]);
			}
		}

		/* Align for `__outputs_len` field */
		_ALIGN(ctx->at, 8);

		/* Write `__outputs_len` field */
		{
			const uint32_t tmp_val = (uint32_t) p___outputs_len;

			memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
			ctx->at += 32;
		}

		/* Align for `outputs` field */
		_ALIGN(ctx->at, 8);

		/* Write `outputs` field */
		{
			uint32_t i;

			for (i = 0; i < (uint32_t) p___outputs_len; ++i) {
				/* Align for `[i]` field */
				_ALIGN(ctx->at, 8);

				/* Write `[i]` field */
				_write_c_str(ctx, p_outputs[i]);
			}
		}

		/* Align for `__internal_len` field */
		_ALIGN(ctx->at, 8);

		/* Write `__internal_len` field */
		{
			const uint32_t tmp_val = (uint32_t) p___internal_len;

			memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
			ctx->at += 32;
		}

		/* Align for `internal` field */
		_ALIGN(ctx->at, 8);

		/* Write `internal` field */
		{
			uint32_t i;

			for (i = 0; i < (uint32_t) p___internal_len; ++i) {
				/* Align for `[i]` field */
				_ALIGN(ctx->at, 8);

				/* Write `[i]` field */
				_write_c_str(ctx, p_internal[i]);
			}
		}

		/* Align for `__internalFB_len` field */
		_ALIGN(ctx->at, 8);

		/* Write `__internalFB_len` field */
		{
			const uint32_t tmp_val = (uint32_t) p___internalFB_len;

			memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
			ctx->at += 32;
		}

		/* Align for `internalFB` field */
		_ALIGN(ctx->at, 8);

		/* Write `internalFB` field */
		{
			uint32_t i;

			for (i = 0; i < (uint32_t) p___internalFB_len; ++i) {
				/* Align for `[i]` field */
				_ALIGN(ctx->at, 8);

				/* Write `[i]` field */
				_write_c_str(ctx, p_internalFB[i]);
			}
		}
	}
}

static void _serialize_er_default_outputData(void * const vctx,
	const char * const p_typeName,
	const char * const p_instanceName,
	const uint64_t p_dataId,
	const char * const p_value)
{
	struct barectf_ctx * const ctx = _FROM_VOID_PTR(struct barectf_ctx, vctx);

	/* Serialize header */
	_serialize_er_header_default(ctx, 2);

	/* Write payload structure */
	{
		/* Align for payload structure */
		_ALIGN(ctx->at, 64);

		/* Align for `typeName` field */
		_ALIGN(ctx->at, 8);

		/* Write `typeName` field */
		_write_c_str(ctx, p_typeName);

		/* Align for `instanceName` field */
		_ALIGN(ctx->at, 8);

		/* Write `instanceName` field */
		_write_c_str(ctx, p_instanceName);

		/* Align for `dataId` field */
		_ALIGN(ctx->at, 64);

		/* Write `dataId` field */
		{
			const uint64_t tmp_val = (uint64_t) p_dataId;

			memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
			ctx->at += 64;
		}

		/* Align for `value` field */
		_ALIGN(ctx->at, 8);

		/* Write `value` field */
		_write_c_str(ctx, p_value);
	}
}

static void _serialize_er_default_receiveInputEvent(void * const vctx,
	const char * const p_typeName,
	const char * const p_instanceName,
	const uint64_t p_eventId)
{
	struct barectf_ctx * const ctx = _FROM_VOID_PTR(struct barectf_ctx, vctx);

	/* Serialize header */
	_serialize_er_header_default(ctx, 3);

	/* Write payload structure */
	{
		/* Align for payload structure */
		_ALIGN(ctx->at, 64);

		/* Align for `typeName` field */
		_ALIGN(ctx->at, 8);

		/* Write `typeName` field */
		_write_c_str(ctx, p_typeName);

		/* Align for `instanceName` field */
		_ALIGN(ctx->at, 8);

		/* Write `instanceName` field */
		_write_c_str(ctx, p_instanceName);

		/* Align for `eventId` field */
		_ALIGN(ctx->at, 64);

		/* Write `eventId` field */
		{
			const uint64_t tmp_val = (uint64_t) p_eventId;

			memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
			ctx->at += 64;
		}
	}
}

static void _serialize_er_default_sendOutputEvent(void * const vctx,
	const char * const p_typeName,
	const char * const p_instanceName,
	const uint64_t p_eventId)
{
	struct barectf_ctx * const ctx = _FROM_VOID_PTR(struct barectf_ctx, vctx);

	/* Serialize header */
	_serialize_er_header_default(ctx, 4);

	/* Write payload structure */
	{
		/* Align for payload structure */
		_ALIGN(ctx->at, 64);

		/* Align for `typeName` field */
		_ALIGN(ctx->at, 8);

		/* Write `typeName` field */
		_write_c_str(ctx, p_typeName);

		/* Align for `instanceName` field */
		_ALIGN(ctx->at, 8);

		/* Write `instanceName` field */
		_write_c_str(ctx, p_instanceName);

		/* Align for `eventId` field */
		_ALIGN(ctx->at, 64);

		/* Write `eventId` field */
		{
			const uint64_t tmp_val = (uint64_t) p_eventId;

			memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
			ctx->at += 64;
		}
	}
}

static uint32_t _er_size_default_inputData(void * const vctx,
	const char * const p_typeName,
	const char * const p_instanceName,
	const char * const p_value)
{
	struct barectf_ctx * const ctx = _FROM_VOID_PTR(struct barectf_ctx, vctx);
	uint32_t at = ctx->at;

	/* Add header structure size */
	{
		/* Align for header structure */
		_ALIGN(at, 8);

		/* Align for `id` field */
		_ALIGN(at, 8);

		/* Add `id` bit array field's size */
		at += 64;

		/* Align for `timestamp` field */
		_ALIGN(at, 8);

		/* Add `timestamp` bit array field's size */
		at += 64;
	}

	/* Add payload structure size */
	{
		/* Align for payload structure */
		_ALIGN(at, 64);

		/* Align for `typeName` field */
		_ALIGN(at, 8);

		/* Add `typeName` string field's size */
		at += _BYTES_TO_BITS(strlen(p_typeName) + 1);

		/* Align for `instanceName` field */
		_ALIGN(at, 8);

		/* Add `instanceName` string field's size */
		at += _BYTES_TO_BITS(strlen(p_instanceName) + 1);

		/* Align for `dataId` field */
		_ALIGN(at, 64);

		/* Add `dataId` bit array field's size */
		at += 64;

		/* Align for `value` field */
		_ALIGN(at, 8);

		/* Add `value` string field's size */
		at += _BYTES_TO_BITS(strlen(p_value) + 1);
	}

	return at - ctx->at;
}

static uint32_t _er_size_default_instanceData(void * const vctx,
	const char * const p_typeName,
	const char * const p_instanceName,
	const uint32_t p___inputs_len,
	const char * const * const p_inputs,
	const uint32_t p___outputs_len,
	const char * const * const p_outputs,
	const uint32_t p___internal_len,
	const char * const * const p_internal,
	const uint32_t p___internalFB_len,
	const char * const * const p_internalFB)
{
	struct barectf_ctx * const ctx = _FROM_VOID_PTR(struct barectf_ctx, vctx);
	uint32_t at = ctx->at;

	/* Add header structure size */
	{
		/* Align for header structure */
		_ALIGN(at, 8);

		/* Align for `id` field */
		_ALIGN(at, 8);

		/* Add `id` bit array field's size */
		at += 64;

		/* Align for `timestamp` field */
		_ALIGN(at, 8);

		/* Add `timestamp` bit array field's size */
		at += 64;
	}

	/* Add payload structure size */
	{
		/* Align for payload structure */
		_ALIGN(at, 8);

		/* Align for `typeName` field */
		_ALIGN(at, 8);

		/* Add `typeName` string field's size */
		at += _BYTES_TO_BITS(strlen(p_typeName) + 1);

		/* Align for `instanceName` field */
		_ALIGN(at, 8);

		/* Add `instanceName` string field's size */
		at += _BYTES_TO_BITS(strlen(p_instanceName) + 1);

		/* Align for `__inputs_len` field */
		_ALIGN(at, 8);

		/* Add `__inputs_len` bit array field's size */
		at += 32;

		/* Align for `inputs` field */
		_ALIGN(at, 8);

		/* Write `inputs` field */
		{
			uint32_t i;

			for (i = 0; i < (uint32_t) p___inputs_len; ++i) {
				/* Align for `[i]` field */
				_ALIGN(at, 8);

				/* Add `[i]` string field's size */
				at += _BYTES_TO_BITS(strlen(p_inputs[i]) + 1);
			}
		}

		/* Align for `__outputs_len` field */
		_ALIGN(at, 8);

		/* Add `__outputs_len` bit array field's size */
		at += 32;

		/* Align for `outputs` field */
		_ALIGN(at, 8);

		/* Write `outputs` field */
		{
			uint32_t i;

			for (i = 0; i < (uint32_t) p___outputs_len; ++i) {
				/* Align for `[i]` field */
				_ALIGN(at, 8);

				/* Add `[i]` string field's size */
				at += _BYTES_TO_BITS(strlen(p_outputs[i]) + 1);
			}
		}

		/* Align for `__internal_len` field */
		_ALIGN(at, 8);

		/* Add `__internal_len` bit array field's size */
		at += 32;

		/* Align for `internal` field */
		_ALIGN(at, 8);

		/* Write `internal` field */
		{
			uint32_t i;

			for (i = 0; i < (uint32_t) p___internal_len; ++i) {
				/* Align for `[i]` field */
				_ALIGN(at, 8);

				/* Add `[i]` string field's size */
				at += _BYTES_TO_BITS(strlen(p_internal[i]) + 1);
			}
		}

		/* Align for `__internalFB_len` field */
		_ALIGN(at, 8);

		/* Add `__internalFB_len` bit array field's size */
		at += 32;

		/* Align for `internalFB` field */
		_ALIGN(at, 8);

		/* Write `internalFB` field */
		{
			uint32_t i;

			for (i = 0; i < (uint32_t) p___internalFB_len; ++i) {
				/* Align for `[i]` field */
				_ALIGN(at, 8);

				/* Add `[i]` string field's size */
				at += _BYTES_TO_BITS(strlen(p_internalFB[i]) + 1);
			}
		}
	}

	return at - ctx->at;
}

static uint32_t _er_size_default_outputData(void * const vctx,
	const char * const p_typeName,
	const char * const p_instanceName,
	const char * const p_value)
{
	struct barectf_ctx * const ctx = _FROM_VOID_PTR(struct barectf_ctx, vctx);
	uint32_t at = ctx->at;

	/* Add header structure size */
	{
		/* Align for header structure */
		_ALIGN(at, 8);

		/* Align for `id` field */
		_ALIGN(at, 8);

		/* Add `id` bit array field's size */
		at += 64;

		/* Align for `timestamp` field */
		_ALIGN(at, 8);

		/* Add `timestamp` bit array field's size */
		at += 64;
	}

	/* Add payload structure size */
	{
		/* Align for payload structure */
		_ALIGN(at, 64);

		/* Align for `typeName` field */
		_ALIGN(at, 8);

		/* Add `typeName` string field's size */
		at += _BYTES_TO_BITS(strlen(p_typeName) + 1);

		/* Align for `instanceName` field */
		_ALIGN(at, 8);

		/* Add `instanceName` string field's size */
		at += _BYTES_TO_BITS(strlen(p_instanceName) + 1);

		/* Align for `dataId` field */
		_ALIGN(at, 64);

		/* Add `dataId` bit array field's size */
		at += 64;

		/* Align for `value` field */
		_ALIGN(at, 8);

		/* Add `value` string field's size */
		at += _BYTES_TO_BITS(strlen(p_value) + 1);
	}

	return at - ctx->at;
}

static uint32_t _er_size_default_receiveInputEvent(void * const vctx,
	const char * const p_typeName,
	const char * const p_instanceName)
{
	struct barectf_ctx * const ctx = _FROM_VOID_PTR(struct barectf_ctx, vctx);
	uint32_t at = ctx->at;

	/* Add header structure size */
	{
		/* Align for header structure */
		_ALIGN(at, 8);

		/* Align for `id` field */
		_ALIGN(at, 8);

		/* Add `id` bit array field's size */
		at += 64;

		/* Align for `timestamp` field */
		_ALIGN(at, 8);

		/* Add `timestamp` bit array field's size */
		at += 64;
	}

	/* Add payload structure size */
	{
		/* Align for payload structure */
		_ALIGN(at, 64);

		/* Align for `typeName` field */
		_ALIGN(at, 8);

		/* Add `typeName` string field's size */
		at += _BYTES_TO_BITS(strlen(p_typeName) + 1);

		/* Align for `instanceName` field */
		_ALIGN(at, 8);

		/* Add `instanceName` string field's size */
		at += _BYTES_TO_BITS(strlen(p_instanceName) + 1);

		/* Align for `eventId` field */
		_ALIGN(at, 64);

		/* Add `eventId` bit array field's size */
		at += 64;
	}

	return at - ctx->at;
}

static uint32_t _er_size_default_sendOutputEvent(void * const vctx,
	const char * const p_typeName,
	const char * const p_instanceName)
{
	struct barectf_ctx * const ctx = _FROM_VOID_PTR(struct barectf_ctx, vctx);
	uint32_t at = ctx->at;

	/* Add header structure size */
	{
		/* Align for header structure */
		_ALIGN(at, 8);

		/* Align for `id` field */
		_ALIGN(at, 8);

		/* Add `id` bit array field's size */
		at += 64;

		/* Align for `timestamp` field */
		_ALIGN(at, 8);

		/* Add `timestamp` bit array field's size */
		at += 64;
	}

	/* Add payload structure size */
	{
		/* Align for payload structure */
		_ALIGN(at, 64);

		/* Align for `typeName` field */
		_ALIGN(at, 8);

		/* Add `typeName` string field's size */
		at += _BYTES_TO_BITS(strlen(p_typeName) + 1);

		/* Align for `instanceName` field */
		_ALIGN(at, 8);

		/* Add `instanceName` string field's size */
		at += _BYTES_TO_BITS(strlen(p_instanceName) + 1);

		/* Align for `eventId` field */
		_ALIGN(at, 64);

		/* Add `eventId` bit array field's size */
		at += 64;
	}

	return at - ctx->at;
}

/* Trace (data stream type `default`, event record type `inputData`) */
void barectf_default_trace_inputData(struct barectf_default_ctx * const sctx,
	const char * const p_typeName,
	const char * const p_instanceName,
	const uint64_t p_dataId,
	const char * const p_value)
{
	struct barectf_ctx * const ctx = &sctx->parent;
	uint32_t er_size;

	/* Save timestamp */
	sctx->cur_last_event_ts = ctx->cbs.default_clock_get_value(ctx->data);

	if (!ctx->is_tracing_enabled) {
		goto end;
	}

	/* We can alter the packet */
	ctx->in_tracing_section = 1;

	/* Compute event record size */
	er_size = _er_size_default_inputData(_TO_VOID_PTR(ctx), p_typeName, p_instanceName, p_value);

	/* Is there enough space to serialize? */
	if (!_reserve_er_space(_TO_VOID_PTR(ctx), er_size)) {
		/* no: forget this */
		ctx->in_tracing_section = 0;
		goto end;
	}

	/* Serialize event record */
	_serialize_er_default_inputData(_TO_VOID_PTR(ctx), p_typeName, p_instanceName, p_dataId, p_value);

	/* Commit event record */
	_commit_er(_TO_VOID_PTR(ctx));

	/* Not tracing anymore */
	ctx->in_tracing_section = 0;

end:
	return;
}

/* Trace (data stream type `default`, event record type `instanceData`) */
void barectf_default_trace_instanceData(struct barectf_default_ctx * const sctx,
	const char * const p_typeName,
	const char * const p_instanceName,
	const uint32_t p___inputs_len,
	const char * const * const p_inputs,
	const uint32_t p___outputs_len,
	const char * const * const p_outputs,
	const uint32_t p___internal_len,
	const char * const * const p_internal,
	const uint32_t p___internalFB_len,
	const char * const * const p_internalFB)
{
	struct barectf_ctx * const ctx = &sctx->parent;
	uint32_t er_size;

	/* Save timestamp */
	sctx->cur_last_event_ts = ctx->cbs.default_clock_get_value(ctx->data);

	if (!ctx->is_tracing_enabled) {
		goto end;
	}

	/* We can alter the packet */
	ctx->in_tracing_section = 1;

	/* Compute event record size */
	er_size = _er_size_default_instanceData(_TO_VOID_PTR(ctx), p_typeName, p_instanceName, p___inputs_len, p_inputs, p___outputs_len, p_outputs, p___internal_len, p_internal, p___internalFB_len, p_internalFB);

	/* Is there enough space to serialize? */
	if (!_reserve_er_space(_TO_VOID_PTR(ctx), er_size)) {
		/* no: forget this */
		ctx->in_tracing_section = 0;
		goto end;
	}

	/* Serialize event record */
	_serialize_er_default_instanceData(_TO_VOID_PTR(ctx), p_typeName, p_instanceName, p___inputs_len, p_inputs, p___outputs_len, p_outputs, p___internal_len, p_internal, p___internalFB_len, p_internalFB);

	/* Commit event record */
	_commit_er(_TO_VOID_PTR(ctx));

	/* Not tracing anymore */
	ctx->in_tracing_section = 0;

end:
	return;
}

/* Trace (data stream type `default`, event record type `outputData`) */
void barectf_default_trace_outputData(struct barectf_default_ctx * const sctx,
	const char * const p_typeName,
	const char * const p_instanceName,
	const uint64_t p_dataId,
	const char * const p_value)
{
	struct barectf_ctx * const ctx = &sctx->parent;
	uint32_t er_size;

	/* Save timestamp */
	sctx->cur_last_event_ts = ctx->cbs.default_clock_get_value(ctx->data);

	if (!ctx->is_tracing_enabled) {
		goto end;
	}

	/* We can alter the packet */
	ctx->in_tracing_section = 1;

	/* Compute event record size */
	er_size = _er_size_default_outputData(_TO_VOID_PTR(ctx), p_typeName, p_instanceName, p_value);

	/* Is there enough space to serialize? */
	if (!_reserve_er_space(_TO_VOID_PTR(ctx), er_size)) {
		/* no: forget this */
		ctx->in_tracing_section = 0;
		goto end;
	}

	/* Serialize event record */
	_serialize_er_default_outputData(_TO_VOID_PTR(ctx), p_typeName, p_instanceName, p_dataId, p_value);

	/* Commit event record */
	_commit_er(_TO_VOID_PTR(ctx));

	/* Not tracing anymore */
	ctx->in_tracing_section = 0;

end:
	return;
}

/* Trace (data stream type `default`, event record type `receiveInputEvent`) */
void barectf_default_trace_receiveInputEvent(struct barectf_default_ctx * const sctx,
	const char * const p_typeName,
	const char * const p_instanceName,
	const uint64_t p_eventId)
{
	struct barectf_ctx * const ctx = &sctx->parent;
	uint32_t er_size;

	/* Save timestamp */
	sctx->cur_last_event_ts = ctx->cbs.default_clock_get_value(ctx->data);

	if (!ctx->is_tracing_enabled) {
		goto end;
	}

	/* We can alter the packet */
	ctx->in_tracing_section = 1;

	/* Compute event record size */
	er_size = _er_size_default_receiveInputEvent(_TO_VOID_PTR(ctx), p_typeName, p_instanceName);

	/* Is there enough space to serialize? */
	if (!_reserve_er_space(_TO_VOID_PTR(ctx), er_size)) {
		/* no: forget this */
		ctx->in_tracing_section = 0;
		goto end;
	}

	/* Serialize event record */
	_serialize_er_default_receiveInputEvent(_TO_VOID_PTR(ctx), p_typeName, p_instanceName, p_eventId);

	/* Commit event record */
	_commit_er(_TO_VOID_PTR(ctx));

	/* Not tracing anymore */
	ctx->in_tracing_section = 0;

end:
	return;
}

/* Trace (data stream type `default`, event record type `sendOutputEvent`) */
void barectf_default_trace_sendOutputEvent(struct barectf_default_ctx * const sctx,
	const char * const p_typeName,
	const char * const p_instanceName,
	const uint64_t p_eventId)
{
	struct barectf_ctx * const ctx = &sctx->parent;
	uint32_t er_size;

	/* Save timestamp */
	sctx->cur_last_event_ts = ctx->cbs.default_clock_get_value(ctx->data);

	if (!ctx->is_tracing_enabled) {
		goto end;
	}

	/* We can alter the packet */
	ctx->in_tracing_section = 1;

	/* Compute event record size */
	er_size = _er_size_default_sendOutputEvent(_TO_VOID_PTR(ctx), p_typeName, p_instanceName);

	/* Is there enough space to serialize? */
	if (!_reserve_er_space(_TO_VOID_PTR(ctx), er_size)) {
		/* no: forget this */
		ctx->in_tracing_section = 0;
		goto end;
	}

	/* Serialize event record */
	_serialize_er_default_sendOutputEvent(_TO_VOID_PTR(ctx), p_typeName, p_instanceName, p_eventId);

	/* Commit event record */
	_commit_er(_TO_VOID_PTR(ctx));

	/* Not tracing anymore */
	ctx->in_tracing_section = 0;

end:
	return;
}
